import lts
import sys
import os

def parse (args_list, minargs, maxargs) :
	# Chequeo la cantidad de argumentos ingresados.
	if len(args_list) < minargs or len(args_list) > maxargs:
		print "The number of arguments is incorrect!"
		sys.exit(0)
	# Valor de retorno.
	result = dict()
	result["system"] = []
	result["fault_set"] = set([])
	result["sync_set"] = set([])
	result["graphic"] = False
	result["heuristic_level"] = 0
	result["time"] = False
	result["verbose"] = False
	result["mode"] = 0
	# Recorro la lista de argumentos.
	for arg in args_list:
		if arg.startswith("-s="):
			for s in ((arg.split('='))[1]).split(','):
				if not s:
					print "\nThe argument '-s' can not be empty."
					sys.exit(0)
				try:
					result["system"].append(lts.from_file(s))
				except lts.LTSError, (msg,e):
					print "\nError in subsystem:", s
					print msg,e
					sys.exit(0)
		elif arg.startswith("-f="):
			f_list = ((arg.split('='))[1]).split(',')
			if not f_list or not f_list[0]:
				print "\nWarning! The fault list is empty. There will be no checking!"
			else:
				for f in f_list:
					if not f.startswith('f'):
						print "\nIncorrect fault name:", f
						sys.exit(0)
					else:
						result["fault_set"].add(f)
		elif arg.startswith("-c="):
			for e in ((arg.split('='))[1]).split(','):
				if e and not e.startswith('c') and not e.startswith('o'):
					print "\nIncorrect synchronization event name:", e
					sys.exit(0)
				elif e:
					result["sync_set"].add(e)
		elif arg.startswith("-h="):
			try:
				result["heuristic_level"] = int((arg.split('='))[1])
			except ValueError:
				print "Invalid value for arguement '-h'."
				sys.exit(0)
			if result["heuristic_level"] < 0 or result["heuristic_level"] > 6:
				print "Values for '-h' go from 0 to 6."
				sys.exit(0)
		elif arg.startswith("-m="):
			try:
				result["mode"] = int((arg.split('='))[1])
			except ValueError:
				print "Invalid value for argument '-m'."
				sys.exit(0)
			if result["mode"] < 0 or result["mode"] > 0:
				print "Argument '-m' only support value 0."
				sys.exit(0)
		elif arg == "-g":
				result["graphic"] = True
		elif arg == "-t":
			result["time"] = True
		elif arg == "-v":
			result["verbose"] = True
		else:
			print "\nInvalid argument:", arg
			sys.exit(0)
	return result
